#include "Random Tree Generator.h"

RTG::RTG(int rangeLimit1)
{
	rangeLimit = rangeLimit1;
	srand(time(NULL));
}


void RTG::Generate()
{
	int amount = rand()%rangeLimit;
	string randomString = ItoS(rand()%32767 + 1);
	Root = new Tree(new Node(randomString , false));
	TreeVector.push_back(Root);
	int index;//index in the TreeVector array
	Tree* ChosenNode;//the node to which we add a new node
	for(int i=0; i<amount; i++)
	{
		randomString = ItoS(rand()%32767 + 1);
		Tree *newNode = new Tree( new Node(  randomString , false));
		index = rand() % TreeVector.size();
		ChosenNode = TreeVector[index];
		ChosenNode->AddLeftTree(newNode);
		TreeVector.push_back(newNode);
	}
}

void RTG::Clear()
{
	Root = NULL;
	TreeVector.clear();
}

Tree* RTG::getRoot()
{
	return Root;
}

string RTG::ItoS(int num)
{
	string s;
	while(num != 0)
	{
		s.insert( s.begin(), '0'+num%10);
		num/=10;
	}
    return s;
}
